const { Service } = require("egg");

class resourcesService extends Service {
    //添加资源
    async addResources(body) {
            try {
                await this.app.model.Resources.create({
                    title: body.title,
                    code: body.code,
                    url: body.url
                })
                return true
            } catch (error) {
                return false
            }
        }
        //获取资源
    async getResources(query) {
            try {
                // let id = query.id
                // if(id){
                //     return await this.app.model.Resources.findOne({
                //         where:{
                //             id
                //         }
                //     })
                // }else{
                    return await this.app.model.Resources.findAll()
                // }

            } catch (error) {
                return null
            }
        }
        //删除资源
    async deleteResources(id) {
            try {
                await this.app.model.Resources.destroy({
                    where: {
                        id
                    }
                })
                return true
            } catch (error) {
                return false
            }
        }
        //更新资源
    async updateResources(id, body) {
        try {
            const resources = {
                title: body.title,
                code: body.code,
                url: body.url
            }
            await this.app.model.Resources.update(resources, {
                where: {
                    id
                }
            })
            return true
        } catch (error) {
            return false
        }
    }
}

module.exports = resourcesService